package org.jivesoftware;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


@Provider 
@Consumes("application/json") 
public class JsonReaderProvider implements MessageBodyReader<Object >{
   


    private final Gson gson; 
    
    public JsonReaderProvider(){
    	gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setDateFormat( 
        "yyyy-MM-dd").create();  
    }

	@Override
	public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
			MediaType arg3) {
		// TODO Auto-generated method stub
		boolean a = MediaType.APPLICATION_XML_TYPE.isCompatible(MediaType.APPLICATION_JSON_TYPE);
		if(arg3.isCompatible(MediaType.APPLICATION_JSON_TYPE))
			return true;
		return false;
	}

	@Override
	public Object  readFrom(Class<Object > arg0, Type arg1, Annotation[] arg2,
			MediaType arg3, MultivaluedMap<String, String> arg4,
			InputStream arg5) throws IOException, WebApplicationException {
		// TODO Auto-generated method stub
		return gson.fromJson(new InputStreamReader(arg5,"UTF-8"), arg1);
	}
    

}
